<template>
  <div class="taskSelect-root mc-table">
    <el-input style="width: 50%" class="ctlTask-body-head__search" v-model="searchParams.taskName" @keyup.enter.native="searchBtn" placeholder="输入任务名称进行搜索">
      <el-button slot="append" icon="el-icon-search" @click="searchBtn"></el-button>
    </el-input>
    <el-table
      stripe
      height="470px"
      border
      size="mini"
      :data="tableData"
      style="width: 100%">
      <el-table-column
        prop="taskName"
        label="任务名称"
        width="180">
      </el-table-column>
      <el-table-column
        prop="startDate"
        label="开始日期"
      >
      </el-table-column>
      <el-table-column
        prop="endDate"
        label="结束日期">
      </el-table-column>

      <el-table-column
        label="操作"
        width="120">
        <template slot-scope="scope">
          <el-button
            @click.native.prevent="selectData(scope.row)"
            type="text"
            size="small">
            选择
          </el-button>
        </template>
      </el-table-column>


    </el-table>
    <el-pagination
      small
      layout="prev, pager, next"
      :total="pageInfo.total"
      :current-page="pageInfo.pageNum"
      :page-size="pageInfo.pageSize"
      @current-change="pageChange"
    >
    </el-pagination>
  </div>
</template>

<script>
import { reactive, toRefs, onMounted, ref, watch, computed } from '@vue/composition-api'
import { resultByTaskId, taskList } from '@/api/industry/ctl'
import loading from '@/utils/loading'

export default {
  name: 'taskSelect',
  setup(props, content) {
    let data = reactive({
      pageInfo: {
        total: 0,
        pageNum: 1,
        pageSize: 9
      },
      searchParams: {
        taskName: ''
      },
      tableData: []
    })
    const pageChange = async(currentPage) => {
      data.pageInfo.pageNum = currentPage
      await loadListData()
    }
    watch(() => data.searchParams.taskName, () => {
      loadListData()
    })
    const loadListData = async() => {
      //let load = loading(document.getElementById('ctlTask-list'))
      try {
        let response = await taskList({ ...data.searchParams, ...data.pageInfo })
        data.tableData = response.rows
        data.pageInfo.total = response.total
      } finally {
        //  load.close()
      }
    }
    onMounted(async() => {
      data.pageInfo.pageNum = 1
      await loadListData()
    })
    const searchBtn = async() => {
      data.pageInfo.pageNum = 1
      await loadListData()
    }
    const selectData = async(item) => {
      content.emit('selectData', item)
    }
    return { ...toRefs(data), pageChange, searchBtn, selectData }
  }
}
</script>

<style lang="scss">
.taskSelect-root {
  height: 100%;
}
</style>
